﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GazpromTestProject
{
    /// <summary>
    /// Summary description for SqlRandomTest
    /// </summary>
    [TestClass]
    public class SqlRandomTest
    {
        public SqlRandomTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [DataSource("System.Data.SqlClient", Constant.LoadTestConnectionString, "TestQueryDefs", DataAccessMethod.Random)]
        public void RunRandomTestQuery()
        {
            if (TestContext.DataRow["System"] == null || string.IsNullOrEmpty(TestContext.DataRow["System"].ToString()))
            {
                Assert.Inconclusive("No system data information");
                return;
            }
            // Database is empty, we have to wait for more test data
            if (TestContext.DataRow["Sql"] == null || string.IsNullOrEmpty(TestContext.DataRow["Sql"].ToString()))
            {
                Assert.Inconclusive(String.Format("No queries for {0} found", TestContext.DataRow["System"]));
                return;
            }

            string Login = TestContext.DataRow["Login"] as string;

            string Password = TestContext.DataRow["Password"] as string;

            string ConnectionPattern = TestContext.DataRow["ConnectionString"] as string;

            string ConnectionString = String.Format(ConnectionPattern, Login, Password);

            string Sql = TestContext.DataRow["Sql"] as string;

            int RowsCount = DbApi.RunTestQueries(ConnectionString, Sql);
         
            Assert.IsNotNull(RowsCount);            
        }

              
        #region Examples


        
          /*  XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Document;
            
            XmlReader reader = XmlReader.Create(filename,settings);

            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            Assert.IsTrue(doc.HasChildNodes);
            */
            //string sChecksum = string.Empty;
            /* FileInfo fi = new FileInfo(filename);

             if (!fi.Exists)
             {
                 Assert.Fail(String.Format("File with the name {0} not exist", filename));
                 return;
             }

             using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
             {
                 MemoryStream ms = new MemoryStream((int)fileStream.Length);
                 fileStream.CopyTo(ms);
                 // Calculate checksum
                 MD5 md5 = new MD5CryptoServiceProvider();
                 byte[] check = md5.ComputeHash(ms);
                 sChecksum = BitConverter.ToString(check);
             }*/
           
            /*MD5 md5 = new MD5CryptoServiceProvider();
            byte[] check = md5.ComputeHash(filedata);
            sChecksum = BitConverter.ToString(check);*/

            //Assert.AreEqual(checksum, sChecksum);

        #endregion
    }
}
